<?php


namespace app\common\model;

use app\common\model\Common as CommonModel;

class AdminOperate extends CommonModel
{
    protected $pk = 'id';

    /**
     * index页面数据获取
     * */
    public function tableData($post,$field='*',$isPage = true)
    {
        if (isset($post['limit'])) {
            $limit = $post['limit'];
        } else {
            $limit = 10;
        }

        $tableWhere = $this->tableWhere($post);
        $query      = $this
            ->field($field)
            ->where($tableWhere['where'])->whereOr($tableWhere['whereOr'])->order($tableWhere['order']);


        if ($isPage) {
            $list        = $query->paginate([
                'list_rows'=> $limit,//每页数量
                'query' => request()->param(),
            ]);
            $data        = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $page = $list->render();
            $re['count'] = $list->total();
        } else {
            $list        = $query->select();
            $data        = $this->tableFormat($list);         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $re['count'] = count($list);
            $page = [];
        }

        $re['code'] = 1;
        $re['msg']  = '';
        $re['data'] = $data;
        $re['page'] = $page;
        return $re;
    }


    protected function tableWhere($post)
    {
        $where = $whereOr = [];
        if(isset($post['admin_name']) && !empty($post['admin_name'])){
            $where[] = ['admin_name','like','%'.$post['admin_name'].'%'];
        }

        $result['where']   = $where;
        $result['whereOr'] = $whereOr;

        $result['field'] = "*";
        $pk = $this->pk;
        $result['order'] = [$pk => 'desc'];
        return $result;
    }

    protected function tableFormat($list)
    {
        foreach($list as $k=>$v){
            $list[$k]['add_time'] = date("Y-m-d H:i:s",$v['add_time']);
        }
        return $list;
    }

    /*public function handle($param){
        $result = [
            'code' => 0,
            'msg'    => '',
            'data'   => [],
        ];
        $act = $param['act'];

        $data['title'] = $param['title'];

        $data['cate_id'] = $param['cate_id'];

        $Category = new Category();
        $cate_one = $Category->where('id','=',$param['cate_id'])->find();
        $p_str = $cate_one['id'].','.$cate_one['arrparentid'];

        if(in_array(75,explode(',',$p_str))){
            $data['type'] = 1;
        }else{
            $data['type'] = 2;
        }

        if(!empty($param['img_url'])){
            $data['img_url'] = implode(',',$param['img_url']);
        }
        $data['price'] = $param['price'];
        $data['duration'] = isset($param['duration'])?$param['duration']:'';
        $data['effective_date'] = isset($param['effective_date'])?$param['effective_date']:'';
        $data['sign_up_num'] = $param['sign_up_num'];
        $data['content'] = $param['content'];
        $data['content_two'] = isset($param['content_two'])?$param['content_two']:'';
        $data['orderid'] = $param['orderid'];
        if($act == 'add'){
            $data['add_time'] = time();
            $r = $this->save($data);
        }
        if($act == 'edit'){
            $data['update_time'] = time();
            $r = $this->where('id','=',$param['id'])->update($data);
        }
        if(!$r){
            $result['msg'] = '操作失败';
            return $result;
        }
        $result['code'] = 1;
        $result['msg'] = '操作成功';
        return $result;
    }*/


}